


import tkinter as tk
from tkinter import simpledialog

# 创建主窗口
window = tk.Tk()
window.title("To Do List")
window.geometry("800x600")

# 定义列
columns = ["To_Do", "In Progress", "Done"]
frames = {}

# 创建列框架
for col in columns:
    frame = tk.Frame(window, relief=tk.RAISED, borderwidth=1)
    frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    frames[col] = frame
    label = tk.Label(frame, text=col, bg='lightblue', font=('Arial', 12, 'bold'))
    label.pack(fill=tk.X)

# 任务卡片类
class DraggableCard(tk.Frame):
    def __init__(self, master, task_name, priority, due_data, **kwargs):
        super().__init__(master, relief=tk.RAISED, borderwidth=1, **kwargs)
        self.task_name = task_name
        self.priority = priority
        self.due_data = due_data

        self.task_label = tk.Label(self, text=f"Task: {task_name}", anchor='w')
        self.task_label.pack(fill=tk.X)
        self.priority_label = tk.Label(self, text=f"Priority: {priority}", anchor='w')
        self.priority_label.pack(fill=tk.X)
        self.due_data_label = tk.Label(self, text=f"Due data: {due_data}", anchor='w')
        self.due_data_label.pack(fill=tk.X)
        self.edit_button = tk.Button(self, text="edit", command=lambda: edit_task(self.task_name, self))
        self.edit_button.pack(anchor='s')
        self.delete_button = tk.Button(self, text="delete", command=lambda: delete_card(self))
        self.delete_button.pack(anchor='s')

        self.bind("<Button-1>", self.on_press)
        self.bind("<B1-Motion>", self.on_drag)
        self.bind("<ButtonRelease-1>", self.on_release)
        self._drag_data = {"x": 0, "y": 0}
        self.original_frame = master  # 记录原始框架

    def on_press(self, event):
        self._drag_data["x"] = event.x
        self._drag_data["y"] = event.y
        self.lift()  # 将当前卡片提升到最上层

    def on_drag(self, event):
        delta_x = event.x - self._drag_data["x"]
        delta_y = event.y - self._drag_data["y"]
        x = self.winfo_x() + delta_x
        y = self.winfo_y() + delta_y
        self.place(x=x, y=y)

    def on_release(self, event):
        target_widget = window.winfo_containing(event.x_root, event.y_root)
        for col, frame in frames.items():
            if target_widget == frame or target_widget.winfo_parent() == frame:
                self.drop(frame)
                break
        else:
            self.drop(self.original_frame)  # 如果没有找到目标框架，则返回原始框架

    def drop(self, target_frame):
        self.place_forget()
        new_card = DraggableCard(target_frame, self.task_name, self.priority, self.due_data)
        new_card.pack(fill=tk.X, pady=5)
        self.master = target_frame
        self.pack_forget()  # 隐藏原始卡片
        self.original_frame = target_frame  # 更新原始框架

def create_card_dialog():
    dialog = tk.Toplevel(window)
    dialog.title("Create New Task")
    dialog.geometry("400x300")

    task_name_var = tk.StringVar()
    priority_var = tk.StringVar()
    due_data_var = tk.StringVar()

    task_name_label = tk.Label(dialog, text="Task Name:")
    task_name_label.pack(pady=5)
    task_name_entry = tk.Entry(dialog, textvariable=task_name_var)
    task_name_entry.pack(pady=5)

    priority_label = tk.Label(dialog, text="Priority:")
    priority_label.pack(pady=5)
    priority_entry = tk.Entry(dialog, textvariable=priority_var)
    priority_entry.pack(pady=5)

    due_data_label = tk.Label(dialog, text="Due Date:")
    due_data_label.pack(pady=5)
    due_data_entry = tk.Entry(dialog, textvariable=due_data_var)
    due_data_entry.pack(pady=5)

    confirm_button = tk.Button(dialog, text="Confirm", command=lambda: add_task(task_name_var.get(), priority_var.get(), due_data_var.get(), dialog))
    confirm_button.pack(pady=10)

def add_task(task_name, priority, due_data, dialog):
    if not task_name or not priority or not due_data:
        return
    create_card(frames["To_Do"], task_name, priority, due_data)
    dialog.destroy()  # 关闭对话框

def create_card(parent_frame, task_name, priority, due_data):
    card_frame = DraggableCard(parent_frame, task_name, priority, due_data)
    card_frame.pack(fill=tk.X, pady=5)

def delete_card(card_frame):
    card_frame.destroy()

def edit_task(task_name, card_frame):
    new_task_name = simpledialog.askstring("Edit task name", f"Current task: {task_name}\nPlease enter new task name")
    if new_task_name:
        for widget in card_frame.winfo_children():
            if isinstance(widget, tk.Label) and widget.cget('text').startswith("Task:"):
                widget.config(text=f"Task: {new_task_name}")

button = tk.Button(window, text="create", command=create_card_dialog)
button.pack()

window.mainloop()